package com.woniu.cms.client.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.client.core.entity.ClientInfo;
import com.woniu.cms.client.core.service.IClientInfoService;
import com.woniu.cms.client.core.vo.ClientInfoVo;
import com.woniu.common.api.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenjiapeng
 * @since 2022-09-19
 */
@RestController
@RequestMapping("/client-info")
@Api(tags = "客户信息服务")
@Slf4j
public class ClientInfoController {

    @Autowired
    private IClientInfoService iClientInfoService;


    @Autowired(required=false)
    private ClientInfo clientInfo;




    @ApiOperation("根据id删除客户信息")
    @DeleteMapping("/{id}")
    public Result deleteOwner(@PathVariable Integer id){

        boolean b = iClientInfoService.removeById(id);

        return b? Result.ok() :Result.error();

    }

    @ApiOperation("批量删除客户信息")
    @DeleteMapping
    public Result deleteOwners(@RequestBody List<Integer> ids){
        //批量删除
        boolean b = iClientInfoService.removeBatchByIds(ids);
        return b? Result.ok() :Result.error();
    }

    @ApiOperation("修改客户信息")
    @PutMapping
    public Result updateOwner(@RequestBody ClientInfo clientInfo){
        boolean save = iClientInfoService.updateById(clientInfo);
        return save ? Result.ok() :Result.error();
    }

    @ApiOperation("保存客户信息")
    @PostMapping
    public Result saveClientInfo(@RequestBody ClientInfo clientInfo){
        boolean save = iClientInfoService.save(clientInfo);
        return save ? Result.ok() :Result.error();
    }

    @ApiOperation(value = "通过客户姓名查找客户对象")
    @GetMapping
    public  Result findByName(String clientName) {

        System.out.println("clientName = " + clientName);

        QueryWrapper<ClientInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("client_name",clientName);

        ClientInfo clientInfo = iClientInfoService.getOne(queryWrapper);

        return Result.common(clientInfo);
    }

    @ApiOperation(value = "通过客户列表Id查找客户对象")
    @GetMapping("{id}")
    public  Result findById(@PathVariable   Integer id) {

        ClientInfo clientInfo = iClientInfoService.getById(id);

        return Result.common(clientInfo);
    }


    @GetMapping("page")
    @ApiOperation(value = "业主分页的查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前的页数",example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "分页大小",example = "3"),
    })
    public Result pageClientInfo(Integer currentPage, Integer pageSize){

        System.out.println("currentPage = " + currentPage);
        System.out.println("pageSize = " + pageSize);


        IPage<ClientInfo> clientInfoIPage = new Page(currentPage,pageSize);

        System.out.println("clientInfoIPage = " + clientInfoIPage);


        IPage<ClientInfo> page = iClientInfoService.page(clientInfoIPage);

        System.out.println("page = " + page);

        List<ClientInfo> records = page.getRecords();

        System.out.println("records = " + records);

        long total = page.getTotal();

        System.out.println("total = " + total);

        List<ClientInfoVo> clientInfoVos = copyListProperties(records);

        Map<String,Object> map = new HashMap<>();
        map.put("data",clientInfoVos);
        map.put("total",total);

        return Result.common(map);


    }


    private List<ClientInfoVo>  copyListProperties( List<ClientInfo>  records){

        List<ClientInfoVo> clientInfoVos  = MyBeanUtils.copyListProperties(records, ClientInfoVo::new);

        return clientInfoVos;

    }

}

